package com.shekhar.moviestore.domain;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Entity
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Movie {

    @NotNull
    private String title;

    @NotNull
    @Size(max = 4000)
    private String description;

    @NotNull
    private String stars;

    @NotNull
    private String director;

    @Transient
    private CommonsMultipartFile file;
    
    private String fileName; 

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public String getTitle() {
        return this.title;
    }

	public void setTitle(String title) {
        this.title = title;
    }

	public String getDescription() {
        return this.description;
    }

	public void setDescription(String description) {
        this.description = description;
    }

	public String getStars() {
        return this.stars;
    }

	public void setStars(String stars) {
        this.stars = stars;
    }

	public String getDirector() {
        return this.director;
    }

	public void setDirector(String director) {
        this.director = director;
    }

	public CommonsMultipartFile getFile() {
        return this.file;
    }

	public void setFile(CommonsMultipartFile file) {
        this.file = file;
    }

	public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Description: ").append(getDescription()).append(", ");
        sb.append("Director: ").append(getDirector()).append(", ");
        sb.append("File: ").append(getFile()).append(", ");
        sb.append("Stars: ").append(getStars()).append(", ");
        sb.append("Title: ").append(getTitle());
        return sb.toString();
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Movie().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countMovies() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Movie o", Long.class).getSingleResult();
    }

	public static List<Movie> findAllMovies() {
        return entityManager().createQuery("SELECT o FROM Movie o", Movie.class).getResultList();
    }

	public static Movie findMovie(Long id) {
        if (id == null) return null;
        return entityManager().find(Movie.class, id);
    }

	public static List<Movie> findMovieEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Movie o", Movie.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Movie attached = Movie.findMovie(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Movie merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Movie merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
